package cc.vv.btpartyjob.partyservice.activity

import android.content.Intent
import android.support.v4.widget.SwipeRefreshLayout
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.view.BTLoadMoreView
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.baselibrary.view.LKCircleImageView
import cc.vv.btpartyjob.jnimodule.binder.BaseBinder
import cc.vv.btpartyjob.jnimodule.server.OperationServer
import cc.vv.btpartyjob.partyservice.R
import cc.vv.btpartyjob.partyservice.adapter.LiuYanBanDetailAdapter
import cc.vv.btpartyjob.partyservice.delegate.LiuYanBanDetailActivityDelegate
import cc.vv.btpartyjob.partyservice.module.bean.LiuYanHuiFuListBean
import cc.vv.btpartyjob.partyservice.module.resp.LiuYanHuifuListResponseObj
import cc.vv.btpartyjob.partyservice.module.resp.MessageBoardDetailResponseObj
import cc.vv.btpartyjob.partyservice.server.DataPartyServer
import cc.vv.lkimagecomponent2.LKImage
import cc.vv.lkimagecomponent2.lib.mode.ScaleMode
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 * 留言板详情
 * Created by Chen.qi on 2018/10/29.
 */
class LiuYanBanDetailActivity : BaseActivityMVP<LiuYanBanDetailActivityDelegate>()
        , BaseQuickAdapter.OnItemClickListener,
        SwipeRefreshLayout.OnRefreshListener,
        BaseQuickAdapter.RequestLoadMoreListener {

    /**
     * 当前页数
     */
    private var pageNo: Int = 1
    /**
     * 每页条数
     */
    private val pageSize: Int = 10

    /**
     * 网格适配器
     */
    private var mAdapter: LiuYanBanDetailAdapter? = null

    /**
     * 集合数据
     */
    private var mDataList: ArrayList<LiuYanHuiFuListBean>? = null

    private var id: String? = null

    private var headView: View? = null


    override fun initData() {
        try {
            id = intent.getStringExtra("id")
        } catch (e: Exception) {
        }
        viewDelegate?.setTopBarTitle("留言板详情")
        mDataList = ArrayList()
        if (mAdapter == null) {
            headView = LayoutInflater.from(this)
                    .inflate(R.layout.layout_liuyan_head, null)
            mAdapter = LiuYanBanDetailAdapter(R.layout.layout_liuyan_item)
            mAdapter!!.addHeaderView(headView)
            viewDelegate?.setRecyclerViewAdapter(mAdapter)
            mAdapter?.setLoadMoreView(BTLoadMoreView())
            viewDelegate?.setAdapterData(mAdapter, mDataList)
        }
        DataPartyServer.getMessageBoardDetail(id, true)
        DataPartyServer.getMessageBoardHuiFuList(id, pageNo, pageSize, true)
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
                val intent = Intent()
                intent.putExtra("id", id)
                intent.setClass(this@LiuYanBanDetailActivity, SubmitHuiFuActivity::class.java)
                startActivityForResult(intent, 100)
            }
        })
        mAdapter?.onItemClickListener = this
        viewDelegate?.bindRefreshListener(this)
        viewDelegate?.bindLoadMore(mAdapter, this)

    }

    override fun getDelegateClass(): Class<LiuYanBanDetailActivityDelegate> {
        return LiuYanBanDetailActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

    }


    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is MessageBoardDetailResponseObj -> {
                val data = obj.data
                //名称
                val nameView = headView?.findViewById<TextView>(R.id.tv_llhH_userName)
                nameView?.text = data?.name

                //头像
                val headPic = headView?.findViewById<LKCircleImageView>(R.id.liv_llhH_headPic)
                LKImage.load().scale(ScaleMode.CENTER_CROP)
                        .crossFade(300).placeHolder(R.mipmap.icon_def_image)
                        .error(R.mipmap.icon_def_image).load(data.faceUrl).into(headPic)

                //所属支部
                val zhibu = headView?.findViewById<TextView>(R.id.tv_llhH_zhiBu)
                zhibu?.text = data?.branch

                //联系电话
                val phone = headView?.findViewById<TextView>(R.id.tv_llhH_phone)
                phone?.text = data?.mobile

                //title
                val title = headView?.findViewById<TextView>(R.id.tv_llhH_title)
                title?.text = data?.title

                //时间
                val time = headView?.findViewById<TextView>(R.id.tv_llhH_time)
                time?.text = OperationServer.formatStr(data?.createTime)

                //内容
                val content = headView?.findViewById<TextView>(R.id.tv_llhH_content)
                content?.text = data?.conetnt


            }

            is LiuYanHuifuListResponseObj -> {
                viewDelegate?.stopRefreshing()
                if (null == obj.data) {
                    return
                }
                if (1 == pageNo) {
                    mDataList?.clear()
                    if (null != obj.data.records) {
                        mDataList!!.addAll(obj.data.records)
                    }
                    viewDelegate?.setAdapterData(mAdapter, mDataList)
                } else {
                    if (mAdapter?.data?.size!! >= obj.data.total) {
                        //数据全部加载完毕
                        mAdapter?.loadMoreEnd(true)
                        mAdapter?.loadMoreEnd()
                    } else {
                        //成功获取更多数据
                        if (null != obj.data.records) {
                            mDataList!!.addAll(obj.data.records)
                        }
                        mAdapter?.setNewData(mDataList)
                        //加载数据
                        mAdapter?.disableLoadMoreIfNotFullPage()
                        mAdapter?.loadMoreComplete()
                    }
                }
            }
        }
    }


    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
        mAdapter?.loadMoreComplete()
        mAdapter?.loadMoreFail()
    }

    override fun onHttpFailure(url: String?, exceptionStr: String?) {
        super.onHttpFailure(url, exceptionStr)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
    }


    override fun onRefresh() {
        mAdapter?.setEnableLoadMore(false)//这里的作用是防止下拉刷新的时候还可以上拉加载
        pageNo = 1
        DataPartyServer.getMessageBoardHuiFuList(id, pageNo, pageSize, false)
    }

    override fun onLoadMoreRequested() {
        if (mDataList!!.size < 10) {
            mAdapter?.loadMoreComplete()
            mAdapter?.loadMoreEnd(true)
            return
        }
        pageNo++
        DataPartyServer.getMessageBoardHuiFuList(id, pageNo, pageSize, true)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100 && resultCode == 101) {
            pageNo = 1
            DataPartyServer.getMessageBoardHuiFuList(id, pageNo, pageSize, true)
        }
    }
}